var lst_countries = false;
var lst_regions = false;
var lst_codes = {};
var lst_codes_list = [];
var lst_timezones = false;
var current_ir = 0;
var region_index = 7;
var current_editor = false;
var editor_options = false;
var user = false;
var send_sms = true;
var current_mobile = false;
var old_csm = 0;
var arrangement_exptime_confirm = 0;
function onExpand(index) {
    toCookie('profile', index, 0);
}
function onCollapse(index) {
    toCookie('profile', index, 1);
}
//save
function doSaveSettings(form) {
	var data={"8":0,"42":{"url":""}};
	var name = $('#id_lang').attr("comboname");
	var lang = $("input[name='"+name+"']").val();

            if (handleState(data)) {
                if (!handleState(data)) return false;
               	$.ajax({
            		url: baselocation+'/member/saveProfile/',
    				type: 'post',
    				data:{lang:lang},
    				success:function(result){
    		             if (result.code==1) {	
    		            	 showOk('mmb_settings_saved');  
    		            	 location.reload();
							} 
    				}
    			})
               
                if (data[42]['url']) {
                    window.location = data[42]['url'];
                }
            } else {
                showAlert('user_not_found');
            }
        
}
function startChange(type, option) {
    if (type == 'mobile') {
    	 showAlert('alert_sorry_disabled');
         return false;
    } else if (type == 'email') {
        if (checkUserPermission('self_change_email')) {} else {
            showAlert('alert_sorry_disabled');
            return false;
        }
    }
    $('#change_' + type).show();
    if (option == 'select') {
        $('#change_' + type + '_buttons .change_select').show();
        $('#change_' + type + '_buttons .change_ok').hide();
    } else {
        $('#change_' + type + '_buttons .change_select').hide();
        $('#change_' + type + '_buttons .change_ok').show();
    }
    if (type == 'mobile') {
        $('#get_sms_mobile span span').html(format(getTranslation('get_sms_code_on_{phone}'), {
            phone: current_mobile
        }));
    }
    $('#change_' + type).dialog({
        width: 600,
        height: 240,
        title: getTranslation('Edit'),
        buttons: '#change_' + type + '_buttons'
    });
}
function enterCode(type) {
    $('#enter_code_' + type).show();
    $('#enter_code_' + type).dialog({
        width: 300,
        height: 130,
        title: getTranslation('Enter Code'),
        buttons: '#enter_code_' + type + '_buttons'
    });
}
function doEnterCode(type) {
    var val = $('#enter_code_' + type + '_val').val();
    if ((!send_sms && type == 'mobile') || val) {
        $.ajax({
            url: baselocation+'/profile/email/ensure',
            data: {
                'o': 'ec',
                't': type,
                'v': val
            },
            success: function(data) {
                if (data.code==1) {
                    $('#enter_code_' + type + '_val').val('');
                    if (editor_options[type]['new_val']) {
                        showOk('mmb_' + type + '_changed');
                        editor_options[type]['value'] = editor_options[type]['new_val'];
                        editor_options[type]['new_val'] = '';
                        editor_options[type]['ver_code'] = '';
                        editor_options[type]['ver_init'] = '';
                        editor_options[type]['ver_time'] = (data[42] > 0) ? 'now': '';
                    } else {
                        showOk('mmb_' + type + '_verified');
                        editor_options[type]['ver_time'] = 'now';
                    }
                    $('#field_' + type).parent('div').html(prepareEmail(type));
                    if (!send_sms && type == 'mobile') {
                        $('#inner_button_8_mobile').hide();
                        $('#inner_button_9_mobile').hide();
                        $('#inner_edit_mobile').css({
                            'color': 'green'
                        });
                    } else {
                        doClose('enter_code_' + type);
                    }
                } else {
                    showAlert('error_code_is_not_valid');
                }
            }
        });
    }
}

//1 validate
function sendCode(type) {
	 $.ajax({
         url: baselocation+'/profile/sendCode/',
         type: 'post',
         dataType: 'json',
         data: {
             'o': 'sc',
             't': type
         },
         success: function(result) {
    	    if (result.code==1) {
    	        editor_options[type]['ver_init'] = '1';
    	        editor_options[type]['ver_code'] = 'now';
    	        //editor_options[type]['new_val'] = '1';
    	        $('#field_' + type).parent('div').html(prepareEmail(type));
    	        showOk('mmb_code_sent');
    	        enterCode(type);
    	    } else if (result.code==0) {
    	    	showAlert('error_send_email');
    	    } else if (result.code==2) {
    	        showAlert('error_code_sent_please_wait');
    	    }
         }
     });
}
function doClose(type) {
    $('#' + type).dialog('close');
    if (type == 'change_mobile_option' || type == 'change_mobile_code') {
        $('#change_mobile_mc').val('');
    }
}
function resetChange(type) {
    $.messager.confirm(getTranslation('Confirm'), getTranslation('mmb_are_you_sure_cancel_change') + '?',
    function(r) {
        if (r) {
            $.ajax({
                url: '62.npl',
                data: {
                    'o': 'rc',
                    't': type
                },
                success: function(data) {
                    if (handleState(data)) {
                        showOk('mmb_' + type + '_change_canceled');
                        editor_options[type]['ver_code'] = '';
                        editor_options[type]['new_val'] = '';
                        $('#field_' + type).parent('div').html(prepareEmail(type));
                        if (current_editor && current_editor[0] && current_editor[0].target && current_editor[0].target[0]) $(current_editor[0].target[0]).html(prepareEmail(type));
                    }
                }
            });
        }
    });
}
function prepareEmail(type) {
    var options = editor_options[type];
    var value = '<div id="field_' + type + '"><b>' + options.value + '</b>';
    if (options.new_val && options.new_val.length > 0) {
        if (options.ver_code == 'LOST') {
            value += ' ' + getTranslation('mmb_will_be_changed_to') + ' ' + options.new_val + ' ' + getTranslation('mmb_in_3_days') + '</div>';
        } else {
            value += ' ' + getTranslation('mmb_will_be_changed_to') + ' ' + options.new_val + ' ' + getTranslation('mmb_click_to_enter_code_or_change') + '</div>';
        }
    } else if (options.ver_time && options.ver_time.length > 0) {
        value += '</div>';
    } else if (options.ver_init && options.ver_init.length > 0) {
        value += ' <a href="#" onClick="return false">' + getTranslation('mmb_verification_started') + '</a></div>';
    } else if (options.value) {
        value += ' <a href="#" onClick="return false">' + getTranslation('mmb_not_verified') + '</a></div>';
    } else if (type == 'mobile') {
        value += ' <a href="#" onClick="return false" style="color:red;font-weight:bold;">' + getTranslation('mmb_do_verification') + '</a></div>';
    } else {
        value += ' <a href="#" onClick="return false">' + getTranslation('mmb_not_set') + '</a></div>';
    }
    return value;
}
$.extend($.fn.propertygrid.defaults.editors, {
    approveornew: {
        init: function(container, type) {
            if (type == 'first_popup') {
                $('#first_popup .ct').show();
                $('#first_popup').window({
                    title: getTranslation('you_must_fill_form'),
                    width: 530,
                    height: 305,
                    modal: true,
                    closed: true,
                    collapsible: false,
                    minimizable: false,
                    maximizable: false,
                    closable: true,
                    buttons: '#first_popup_buttons',
                    onClose: function() {}
                });
                $('#first_popup_option').val(20);
                $('#first_popup').show().window('open');
            } else {
                var options = editor_options[type];
                var value = options.value;
                if (!value) {
                    if (type == 'mobile') {
                        value = getTranslation('mmb_do_verification');
                    } else {
                        value = getTranslation('mmb_not_set');
                    }
                }
                $('<b id="inner_edit_' + type + '">&nbsp;' + value + '</b> ').appendTo(container);
                if (options.new_val) {
                    if (options.ver_code != 'LOST') {
                        var btn = $('<input id="inner_button_1_' + type + '" type="button" onClick="enterCode(\'' + type + '\');return false;" value="' + getTranslation('Enter Code') + '"/>').appendTo(container);
                        btn.linkbutton({
                            plain: true
                        });
                        var btn = $('<input id="inner_button_2_' + type + '" type="button" onClick="sendCode(\'' + type + '\');return false;" value="' + getTranslation('Send Again') + '"/>').appendTo(container);
                        btn.linkbutton({
                            plain: true
                        });
                    }
                    var btn = $('<input id="inner_button_3_' + type + '" type="button" onClick="resetChange(\'' + type + '\');return false;" value="' + getTranslation('Reset Change') + '"/>').appendTo(container);
                    btn.linkbutton({
                        plain: true
                    });
                } else if (options.ver_time) {
                    var btn = $('<input id="inner_button_4_' + type + '" type="button" onClick="startChange(\'' + type + '\', \'select\');return false;" value="' + getTranslation('Change') + '"/>').appendTo(container);
                    btn.linkbutton();
                } else if (options.ver_init) {
                    var btn = $('<input id="inner_button_5_' + type + '" type="button" onClick="enterCode(\'' + type + '\');return false;" value="' + getTranslation('Enter Code') + '"/>').appendTo(container);
                    btn.linkbutton({
                        plain: true
                    });
                    var btn = $('<input id="inner_button_6_' + type + '" type="button" onClick="sendCode(\'' + type + '\');return false;" value="' + getTranslation('Send Again') + '"/>').appendTo(container);
                    btn.linkbutton({
                        plain: true
                    });
                    var btn = $('<input id="inner_button_7_' + type + '" type="button" onClick="startChange(\'' + type + '\', \'ok\');return false;" value="' + getTranslation('Change') + '"/>').appendTo(container);
                    btn.linkbutton();
                } else {
                    if (options.value) {
                        var btn = $('<input id="inner_button_8_' + type + '" type="button" onClick="sendCode(\'' + type + '\');return false;" value="' + getTranslation('Verify') + '"/>').appendTo(container);
                        btn.linkbutton();
                    }
                    var btn = $('<input id="inner_button_9_' + type + '" type="button" onClick="startChange(\'' + type + '\', \'ok\');return false;" value="' + getTranslation(type == 'mobile' ? 'Process': 'Change') + '"/>').appendTo(container);
                    btn.linkbutton();
                }
            }
            container.rel = type;
            return container;
        },
        destroy: function(target) {
            if (target.rel != 'first_popup') {
                $(target).html(prepareEmail(target.rel));
            }
        },
        getValue: function(target) {},
        setValue: function(target, value) {}
    },
    approveornew: {
        init: function(container, type) {
            if (type == 'first_popup') {
                $('#first_popup .ct').show();
                $('#first_popup').window({
                    title: getTranslation('you_must_fill_form'),
                    width: 530,
                    height: 305,
                    modal: true,
                    closed: true,
                    collapsible: false,
                    minimizable: false,
                    maximizable: false,
                    closable: true,
                    buttons: '#first_popup_buttons',
                    onClose: function() {}
                });
                $('#first_popup_option').val(20);
                $('#first_popup').show().window('open');
            } else {
                var options = editor_options[type];
                var value = options.value;
                if (!value) {
                    if (type == 'mobile') {
                        value = getTranslation('mmb_do_verification');
                    } else {
                        value = getTranslation('mmb_not_set');
                    }
                }
                $('<b id="inner_edit_' + type + '">&nbsp;' + value + '</b> ').appendTo(container);
                if (options.new_val) {
                    if (options.ver_code != 'LOST') {
                        var btn = $('<input id="inner_button_1_' + type + '" type="button" onClick="enterCode(\'' + type + '\');return false;" value="' + getTranslation('Enter Code') + '"/>').appendTo(container);
                        btn.linkbutton({
                            plain: true
                        });
                        var btn = $('<input id="inner_button_2_' + type + '" type="button" onClick="sendCode(\'' + type + '\');return false;" value="' + getTranslation('Send Again') + '"/>').appendTo(container);
                        btn.linkbutton({
                            plain: true
                        });
                    }
                    var btn = $('<input id="inner_button_3_' + type + '" type="button" onClick="resetChange(\'' + type + '\');return false;" value="' + getTranslation('Reset Change') + '"/>').appendTo(container);
                    btn.linkbutton({
                        plain: true
                    });
                } else if (options.ver_time) {
                    var btn = $('<input id="inner_button_4_' + type + '" type="button" onClick="startChange(\'' + type + '\', \'select\');return false;" value="' + getTranslation('Change') + '"/>').appendTo(container);
                    btn.linkbutton();
                } else if (options.ver_init) {
                    var btn = $('<input id="inner_button_5_' + type + '" type="button" onClick="enterCode(\'' + type + '\');return false;" value="' + getTranslation('Enter Code') + '"/>').appendTo(container);
                    btn.linkbutton({
                        plain: true
                    });
                    var btn = $('<input id="inner_button_6_' + type + '" type="button" onClick="sendCode(\'' + type + '\');return false;" value="' + getTranslation('Send Again') + '"/>').appendTo(container);
                    btn.linkbutton({
                        plain: true
                    });
                    var btn = $('<input id="inner_button_7_' + type + '" type="button" onClick="startChange(\'' + type + '\', \'ok\');return false;" value="' + getTranslation('Change') + '"/>').appendTo(container);
                    btn.linkbutton();
                } else {
                    if (options.value) {
                        var btn = $('<input id="inner_button_8_' + type + '" type="button" onClick="sendCode(\'' + type + '\');return false;" value="' + getTranslation('Verify') + '"/>').appendTo(container);
                        btn.linkbutton();
                    }
                    var btn = $('<input id="inner_button_9_' + type + '" type="button" onClick="startChange(\'' + type + '\', \'ok\');return false;" value="' + getTranslation(type == 'mobile' ? 'Process': 'Change') + '"/>').appendTo(container);
                    btn.linkbutton();
                }
            }
            container.rel = type;
            return container;
        },
        destroy: function(target) {
            if (target.rel != 'first_popup') {
                $(target).html(prepareEmail(target.rel));
            }
        },
        getValue: function(target) {},
        setValue: function(target, value) {}
    },
    textarea_nospan: {
        init: function(container, type) {
            var value = editor_options['additional_contacts'];
            $('<textarea id="additional_contacts_editor">' + value + '</textarea>').appendTo(container);
            return container;
        },
        destroy: function(target) {
            editor_options['additional_contacts'] = $('#additional_contacts_editor').val();
        },
        getValue: function(target) {
            return '' + $('#additional_contacts_editor').val() + '';
        },
        setValue: function(target, value) {}
    }
});
$(function() {
	$.ajax({
		url:"/Home/User/getWodeyemianLog/",
		type: 'get',
		dataType: 'json',
		success:function(result){
//			alert(345);			
            console.log(result);		
			//right
			var data={"ucr":{"840":"United States Dollar(USD)"},
					"cl":{"79":"Indonesia","35":"China","10001":"Republic Bitcoin","133":"Venezuela","15":"Vietnam","130":null},
					"rl":{"20":{"1259":"Ahaia","1261":"Attiki","1264":"Drama","1266":"Evros","1268":"Hania","1270":"Ioannina","1272":"Iraklion","1274":"Kavala"}},
					"lnl":{"45":"English","79":"Indonesia","315":"\u4e2d\u570b\u7684","570":"\u0420\u0443\u0441\u0441\u043a\u0438\u0439"},
					"countries-mobiles":[["50","Russia","7","1"],["62","Ukraine","380","1"],["129","Nepal","977","1"],["1","Australia","61","1"],["2","Austria","43","1"]],
					"tz":[{"r":"0","i":"14","n":"(UTC) Western Europe Time, London, Lisbon, Casablanca"}]};
					    
		            lst_countries = data.cl;
		            lst_regions = data.rl;
		            lst_languages = data.lnl;
		            lst_currencies = data.ucr;
		            lst_codes_list = data['countries-mobiles'];
		            lst_timezones = data.tz;
					//init
		        	 data = result;		  
        	 		if (!handleState(data)) return false;
                    user = data[42];
                    send_sms = !can_mobile_no_sms && user[20120];
                    if (!send_sms) $('.send_sms').hide();
                    var options_codes = new Array();
                    if (user[45] == 8) {
                        options_codes.push({
                            'value': 25,
                            'text': 'India +91'
                        });
                    } else {
                        $.each(lst_codes_list,
                        function(id, value) {
                            options_codes.push({
                                'value': value[0],
                                'text': value[1] + ' +' + value[2]
                            });
                            lst_codes[value[0]] = value;
                        });
                    }
                    var code = (user[45] == 8) ? 25 : (typeof(lst_codes[user[6]]) == 'undefined' ? null: user[6]);
                    $('#edit_im').combobox({
                        value: code,
                        editable: false,
                        required: true,
                        multiple: false,
                        data: options_codes,
                        filter: function(q, row) {
                            return row['text'].toLowerCase().indexOf(q.toLowerCase()) != -1;
                        }
                    });
                    $('#edit_im2').combobox({
                        value: code,
                        editable: false,
                        required: true,
                        multiple: false,
                        data: options_codes,
                        filter: function(q, row) {
                            return row['text'].toLowerCase().indexOf(q.toLowerCase()) != -1;
                        }
                    });
                    $('#edit_im3').combobox({
                        value: code,
                        editable: false,
                        required: true,
                        multiple: false,
                        data: options_codes,
                        filter: function(q, row) {
                            return row['text'].toLowerCase().indexOf(q.toLowerCase()) != -1;
                        }
                    });
                    var show_structure_tab = false;
                    if (checkUserPermission('self_set_invalid') && (user[6] == 50 || user[6] == 62 || user[6] == 30)) {
                        if (user[20090] && user[20090] == 2) {
                            $('#can_self_set_invalid_done').show();
                        } else {
                            if (user[20090] && user[20090] == 1) {
                                $('#can_self_set_invalid_in_progress').show();
                            } else if (user[20090] && user[20090] == 3) {
                                $('#can_self_set_invalid_declined').show();
                            }
                            $('#can_self_set_invalid_button').show();
                        }
                        $('#can_self_set_invalid').show();
                        show_structure_tab = true;
                    }
                    if (user[220]) {
                        if (user[220] == 2) {
                            $('#can_approve_2011_submitted').show();
                        } else if (user[220] == 1) {
                            $('#can_approve_2011').show();
                        }
                        show_structure_tab = true;
                    }
                    if (user[221]) {
                        if (user[221] == 2) {
                            $('#can_approve_2012_submitted').show();
                        } else if (user[221] == 1) {
                            $('#can_approve_2012').show();
                        }
                        show_structure_tab = true;
                    }
                    if (user[222]) {
                        if (user[222] == 1) {
                            $('#can_approve_es').show();
                            show_structure_tab = true;
                        }
                    }
                    if (!show_structure_tab) {
                        $('#structure_tab').remove();
                    }
                    var show_parent_tab = false;
                    if ((typeof(user[231]) != 'undefined') && user[231] >= 0) {
                    	show_parent_tab = true;
                    	 if (user[45] == 1000) {
                             $('#just_parent_name').show();
                             $('.your_parent_name').html(user[50]);
                         }
                    }
                    if (!show_parent_tab) {
                        $('#parent_tab').remove();
                    }
		                    initRefLink();
		                    if (user[27].length > 0) {
		                        $('#your_ref_nick').html(user[27]);
		                        $('#has_ref').show();
		                        if (user[260] > 0) {
		                            $('#has_ref_reset').show();
		                        } else {
		                            $('#has_ref_reset').hide();
		                        }
		                        $('#has_noref').hide();
		                        $('#has_norefnochange').hide();
		                    } else if (user[260] > 0) {
		                        $('#your_cr').html(user[28]);
		                        $('#has_noref').show();
		                        $('#has_ref').hide();
		                        $('#has_norefnochange').hide();
		                    } else {
		                        $('#has_norefnochange').show();
		                        $('#has_noref').hide();
		                        $('#has_ref').hide();
		                    }
		                    if (user[0] && user[0].length > 0) {
		                        $('#ref_nick').html(user[0]);
		                        $('#has_ref_nick').show();
		                        $('#has_noref_nick').hide();
		                    } else {
		                        $('#has_noref_nick').show();
		                        $('#has_ref_nick').hide();
		                    }
		                    if (!checkUserPermission('self_set_mobile_login')) {
		                        $('#mobile_tab').remove();
		                    } else if (typeof(user['ml']) != 'undefined' && user['ml']) {
		                        $.each(user.ml,
		                        function(id, val) {
		                            doMobileLoginRow(val.i, val.n, val.t);
		                        });
		                    }
		                    var options_1 = new Array();
		                    if (user[21] == 570) {
		                        $.each(lst_timezones,
		                        function(id, val) {
		                            if (val['r'] == 1) {
		                                options_1.push({
		                                    'value': val['i'],
		                                    'text': val['n']
		                                });
		                            }
		                        });
		                        $.each(lst_timezones,
		                        function(id, val) {
		                            if (val['r'] == 0) {
		                                options_1.push({
		                                    'value': val['i'],
		                                    'text': val['n']
		                                });
		                            }
		                        });
		                    } else {
		                        $.each(lst_timezones,
		                        function(id, val) {
		                            if (val['r'] == 0) {
		                                options_1.push({
		                                    'value': val['i'],
		                                    'text': val['n']
		                                });
		                            }
		                        });
		                        $.each(lst_timezones,
		                        function(id, val) {
		                            if (val['r'] == 1) {
		                                options_1.push({
		                                    'value': val['i'],
		                                    'text': val['n']
		                                });
		                            }
		                        });
		                    }
		                    $('#time_zone').combobox({
		                        multiple: false,
		                        editable: false,
		                        data: options_1,
		                        value: user[53],
		                        filter: function(q, row) {
		                            return row['text'].toLowerCase().indexOf(q.toLowerCase()) != -1;
		                        }
		                    });
		                    if (typeof(user[560]) != 'undefined') {
		                        var h = getTranslation('in_hours');
		                        if (!user[560]['arrangement_exptime_hours']) user[560]['arrangement_exptime_hours'] = 72;
		                        if (typeof(user[560]['not_editable']) == 'undefined' || user[560]['not_editable'] == 0) {
		                            $('#can_send_money').combobox({
		                                multiple: false,
		                                editable: false,
		                                data: new Array({
		                                    'value': 0,
		                                    'text': '72 ' + h
		                                },
		                                {
		                                    'value': 48,
		                                    'text': '48 ' + h
		                                },
		                                {
		                                    'value': 24,
		                                    'text': '24 ' + h
		                                },
		                                {
		                                    'value': 12,
		                                    'text': '12 ' + h
		                                },
		                                {
		                                    'value': 6,
		                                    'text': '6 ' + h
		                                },
		                                {
		                                    'value': 3,
		                                    'text': '3 ' + h
		                                }),
		                                onSelect: function(new_csm) {
		                                    var memo = '';
		                                    if (new_csm.value < old_csm) {
		                                        memo = 'mmb_csm_go_less';
		                                    }
		                                    if (memo) {
		                                        $.messager.confirm(getTranslation('Confirm'), getTranslation(memo),
		                                        function(o) {
		                                            if (o) {
		                                                return true;
		                                            } else {
		                                                $('#can_send_money').combobox('setValue', old_csm);
		                                            }
		                                        });
		                                    }
		                                },
		                                value: user[560]['arrangement_exptime_hours'],
		                                filter: function(q, row) {
		                                    return row['text'].toLowerCase().indexOf(q.toLowerCase()) != -1;
		                                }
		                            });
		                            old_csm = user[560]['arrangement_exptime_hours'];
		                            arrangement_exptime_confirm = typeof(user[560]['cf']) == 'undefined' ? 0 : user[560]['cf'];
		                            $('#profile_settings_ed').show();
		                        } else {
		                            $('#can_send_money').combobox({
		                                multiple: false,
		                                editable: false,
		                                data: new Array({
		                                    'value': user[560]['arrangement_exptime_hours'],
		                                    'text': user[560]['arrangement_exptime_hours'] + ' ' + h
		                                }),
		                                value: user[560]['arrangement_exptime_hours'],
		                                filter: function(q, row) {
		                                    return row['text'].toLowerCase().indexOf(q.toLowerCase()) != -1;
		                                }
		                            });
		                            $('#profile_settings_ed').html(getTranslation('mmb_csm_not_editable_' + user[560]['not_editable']));
		                            $('#profile_settings_ed').show();
		                        }
		                    } else {
		                        $('#profile_settings_csm_tab').remove();
		                    }
		                    var options_1 = new Array();
		                    $.each(lst_languages,
		                    function(id, value) {
		                        options_1.push({
		                            'value': id,
		                            'text': value
		                        });
		                    });
		                    $('#id_lang').combobox({
		                        multiple: false,
		                        editable: false,
		                        data: options_1,
		                        value: user[21] ? user[21] : 570,
		                        filter: function(q, row) {
		                            return row['text'].toLowerCase().indexOf(q.toLowerCase()) != -1;
		                        }
		                    });
		                    if (user[22] > 0) $('#get_emails').attr('checked', true);
		                    if (user[54] > 0) $('#get_ext_sms').attr('checked', true);
		                    if (user[23] > 0) $('#about_show_nick').attr('checked', true);
		                    if (user[57] > 0) $('#hide_my_mobile').attr('checked', true);
		                    if (user[24] > 0) $('#about_show_email').attr('checked', true);
		                    if (user[25] > 0) $('#about_show_mobile').attr('checked', true);
		                    var options_1 = new Array();
		                    var first = 0;
		                    $.each(lst_currencies,
		                    function(id, value) {
		                        if (!first) first = id;
		                        options_1.push({
		                            'value': id,
		                            'text': value
		                        });
		                    });
		                    $('#id_currency').combobox({
		                        multiple: false,
		                        editable: false,
		                        data: options_1,
		                        value: user[37] ? user[37] : first,
		                        filter: function(q, row) {
		                            return row['text'].toLowerCase().indexOf(q.toLowerCase()) != -1;
		                        }
		                    });
		                    $('#approve_2011_currency_span').html(lst_currencies[user[37] ? user[37] : first]);
		                    $('#approve_2011_currency').val(user[37] ? user[37] : first);
		                    $('#approve_2012_currency_span').html(lst_currencies[user[37] ? user[37] : first]);
		                    $('#approve_2012_currency').val(user[37] ? user[37] : first);
		                    if (user[38] > 0) $('#allow_get_children').attr('checked', true);
		                    editor_options = {
		                        'email': {
		                            'value': user[4],
		                            'ver_init': user[29],
		                            'ver_code': user[30],
		                            'ver_time': user[31],
		                            'new_val': user[32]
		                        },
		                        'mobile': {
		                            'value': user[5] ? user[5] : '',
		                            'ver_init': user[33],
		                            'ver_code': user[34],
		                            'ver_time': user[35],
		                            'new_val': user[36]
		                        }
		                    };
		                    //2
		                    $('#profile_edit').propertygrid({
		                        showGroup: true,
		                        showHeader: false,
		                        rowStyler: function(index, row) {
		                            var text = 'background-color:#EFEFEF';
		                            if (index == 4) {
		                                if (editor_options['email']['new_val']) {
		                                    text += ';color:red;';
		                                } else if (editor_options['email']['ver_time']) {
		                                    text += ';color:green;';
		                                } else if (editor_options['email']['ver_init']) {
		                                    text += ';color:blue;';
		                                }
		                                return text;
		                            } else if (index == 5) {
		                                if (editor_options['mobile']['new_val']) {
		                                    text += ';color:red;';
		                                } else if (editor_options['mobile']['ver_time']) {
		                                    text += ';color:green;';
		                                } else if (editor_options['mobile']['ver_init']) {
		                                    text += ';color:blue;';
		                                }
		                                return text;
		                            } else if (index == 3 || index == 6 || index == 9) {
		                                return text;
		                            }
		                        },
		                        onAfterEdit: function(index, field, v) {
		                            if ((index == region_index) && !v['value']) return true;
		                            if (index == region_index) {
		                                $('#profile_edit').propertygrid('updateRow', {
		                                    index: region_index,
		                                    row: {
		                                        'value': lst_regions[current_ic][v['value']] ? lst_regions[current_ic][v['value']] : ''
		                                    }
		                                });
		                                current_ir = v['value'];
		                            }
		                        },
		                        groupFormatter: function(group, rows) {
		                            if (group == getTranslation('General (Click to change)') || group == getTranslation('Contacts') || group == getTranslation('About') || group == getTranslation('Present From')) {
		                                return '<span style="color:#666">' + group + '</span>';
		                            } else if (group > 100) {
		                                return '<span style="color:#3366ff">' + getTranslation('Coordinator') + ' / ' + (lst_countries[group / 100]) + '</span>';
		                            } else {
		                                return '<span style="color:black">' + rows[1].value + ' / ' + (group > 1 ? getTranslation('mmb_parent_parent') : getTranslation('mmb_direct_parent')) + '</span>';
		                            }
		                        }
		                    });
		                    $('#profile_parent').propertygrid({
		                        showGroup: true,
		                        showHeader: false
		                    });
		                    var max_i = doShowBigForm(user);
		                    for (var i = 0; i < max_i; i++) {
		                        if (fromCookie('profile', i) == 1) {
		                            $('#profile_edit').propertygrid('collapseGroup', i);
		                        }
		                    }
		                    if ((!user[5] || (!user[18] && user[45] == 950)) && (user_perms.id_position > 5) && user[20110]) {
		                        var first_popup_option = 0;
		                        if (!user[5] && user[20110]) {
		                            $('#first_popup .mb').show();
		                            first_popup_option = first_popup_option + 10;
		                        }
		                        if (!user[18] && user[45] == 950) {
		                            $('#first_popup .ct').show();
		                            first_popup_option = first_popup_option + 20;
		                        }
		                        $('#first_popup').window({
		                            title: getTranslation('you_must_fill_form'),
		                            width: 530,
		                            height: 305,
		                            modal: true,
		                            closed: true,
		                            collapsible: false,
		                            minimizable: false,
		                            maximizable: false,
		                            closable: false,
		                            buttons: '#first_popup_buttons',
		                            onClose: function() {}
		                        });
		                        $('#first_popup_option').val(first_popup_option);
		                        $('#first_popup').show().window('open');
		                    } else {
		                        var show_load_form = false;
		                        if (!user[39] || !user[40] || !user[41] || !user[42] || !user[43]) {
		                            show_load_form = true;
		                        }
		                        if ((user[3] > 10) && lst_regions[user[39]] && (show_load_form == true || (user[540] == 1) || (user[540] == 5))) {
		                            if (user[540] == 1 || user[540] == 5) {
		                                createUploader(document.getElementById('load_document_popup'),
		                                function() {
		                                    $('#save_document_popup').show();
		                                });
		                                $('#load_document_popup').show();
		                                if (user[540] == 5) {
		                                    $('#load_document_note_popup2').show();
		                                } else {
		                                    $('#load_document_note_popup').show();
		                                    $('#load_document_prenote').show();
		                                }
		                            } else {
		                                $('#save_document_popup').show();
		                                $('#load_document_prenote').show();
		                            }
		                            var options = new Array();
		                            if (lst_regions[user[39]]) {
		                                $.each(lst_regions[user[6]],
		                                function(id, value) {
		                                    options.push({
		                                        'value': id,
		                                        'text': value
		                                    });
		                                });
		                            }
		                            $('#fp_region').combobox({
		                                multiple: false,
		                                data: options,
		                                filter: function(q, row) {
		                                    return row['text'].toLowerCase().indexOf(q.toLowerCase()) != -1;
		                                }
		                            });
		                            if (user[41]) $('#fp_first').val(user[41]);
		                            else if (user[1]) $('#fp_first').val(user[1]);
		                            if (user[42]) $('#fp_last').val(user[42]);
		                            else if (user[2]) $('#fp_last').val(user[2]);
		                            if (user[40]) $('#fp_city').val(user[40]);
		                            else if (user[8]) $('#fp_city').val(user[8]);
		                            if (user[39]) $('#fp_region').combobox('select', user[39]);
		                            else if (user[7]) $('#fp_region').combobox('select', user[7]);
		                            if (user[43]) $('#fp_birth').datebox('setValue', user[43]);
		                            else if (user[10]) $('#fp_birth').datebox('setValue', user[10]);
		                            var h = (user[540] == 1) ? 560 : 400;
		                            var form_title = (show_load_form && (user[540] == 1)) ? 'you_must_put_document_and_fill_form': ((user[540] == 1) ? 'you_must_put_document': 'you_must_fill_form');
		                            if (user[540] == 5) form_title = getTranslation('you_must_put_document');
		                            $('#load_document_form_popup').window({
		                                title: getTranslation(form_title),
		                                width: 500,
		                                height: h,
		                                modal: true,
		                                closed: true,
		                                collapsible: false,
		                                minimizable: false,
		                                maximizable: false,
		                                closable: false,
		                                top: ($(window).height() - h) / 2,
		                                onClose: function() {}
		                            });
		                            $('#load_document_form_popup').show().window('open');
		                        }
		                    }
		                    $('#right_tabs').show().accordion();
		                    $.each($('.panel-title'),
		                    function(id, value) {
		                        $(value).html(getTranslation($(value).html()));
		                    });
		                    $('.combo').css('width','185px');
	}
});

                
        
});
//init
var can_mobile = false;
var can_mobile_no_sms = false;
function doShowBigForm(user) {
    var options_2 = new Array();
    if (lst_regions[user[6]]) {
        $.each(lst_regions[user[6]],
        function(id, value) {
            options_2.push({
                'value': id,
                'text': value
            });
        });
        current_ic = user[6];
    }
    var fields = new Array();
    var is_dec = false;
    var is_ru = user[1200];
    var can_hmm = user[1201];
    can_mobile = user[1300];
    can_mobile_no_sms = user[1301];
    if (can_hmm) {
        $('.settings_hmm').show();
    }
    if (is_dec) {
        fields.push({
            'name': getTranslation('First Name'),
            'value': user[1],
            'group': getTranslation('General (Click to change)')
        });
        fields.push({
            'name': getTranslation('Last Name'),
            'value': user[2],
            'group': getTranslation('General (Click to change)')
        });
    } else {
        fields.push({
            'name': getTranslation('First Name'),
            'value': user[1],
            'group': getTranslation('General (Click to change)'),
            'editor': {
                'type': 'validatebox',
                'options': {
                    'required': true
                }
            }
        });
        fields.push({
            'name': getTranslation('Last Name'),
            'value': user[2],
            'group': getTranslation('General (Click to change)'),
            'editor': {
                'type': 'validatebox',
                'options': {
                    'required': true
                }
            }
        });
    }
    var user_position = toUsersPosition(user[3]);
    if (typeof(user[301]) != 'undefined' && user[303]) {
        user_position = user[303];
    }
    fields.push({
        'name': getTranslation('Status'),
        'value': user[999],
        'group': getTranslation('General (Click to change)')
    });
    fields.push({
        'name': getTranslation('Stage'),
        'value': getTranslation(user[888]+''),
        'group': getTranslation('General (Click to change)')
    });
    if (parseInt(user[1100], 10) != 1) {
        fields.push({
            'name': getTranslation('Email'),
            'value': prepareEmail('email'),
            'group': getTranslation('General (Click to change)'),
            'editor': {
                'type': 'approveornew',
                'options': 'email'
            }
        });
    } else {
        $('#container_about_show_email').hide();
        fields.push({
            'name': getTranslation('Email'),
            'value': getTranslation('not_installed'),
            'group': getTranslation('General (Click to change)')
        });
    }
    fields.push({
        'name': getTranslation('Mobile'),
        'value': prepareEmail('mobile'),
        'group': getTranslation('General (Click to change)'),
        'editor': {
            'type': 'approveornew',
            'options': 'mobile'
        }
    });
    current_mobile = user[5];
    fields.push({
        'name': getTranslation('Country'),
        'value': lst_countries[user[6]],
        'group': getTranslation('General (Click to change)')
    });
    if (1==1) {
        fields.push({
            'name': getTranslation('Region'),
            'value': lst_regions[user[6]] ? lst_regions[user[6]][user[7]] : '',
            'group': getTranslation('General (Click to change)')
        });
        fields.push({
            'name': getTranslation('City'),
            'value': user[8],
            'group': getTranslation('General (Click to change)')
        });
    } else {
        fields.push({
            'name': getTranslation('Region'),
            'value': lst_regions[user[6]] ? lst_regions[user[6]][user[7]] : '',
            'group': getTranslation('General (Click to change)'),
            'editor': {
                'type': 'combobox',
                'options': {
                    'data': options_2,
                    'required': false,
                    editable: false,
                    'multiple': false,
                    'value': user[7],
                    filter: function(q, row) {
                        return row['text'].toLowerCase().indexOf(q.toLowerCase()) != -1;
                    }
                }
            }
        });
        fields.push({
            'name': getTranslation('City'),
            'value': user[8],
            'group': getTranslation('General (Click to change)'),
            'editor': 'text'
        });
    }
    fields.push({
        'name': getTranslation('Registered'),
        'value': user[9],
        'group': getTranslation('General (Click to change)')
    });
    if (is_dec) {
        fields.push({
            'name': getTranslation('Birthday'),
            'value': user[10],
            'group': getTranslation('General (Click to change)')
        });
    } else {
        fields.push({
            'name': getTranslation('Birthday'),
            'value': user[10],
            'group': getTranslation('General (Click to change)'),
            'editor': {
                'type': 'datebox'
            }
        });
    }
    fields.push({
        'name': getTranslation('Skype'),
        'value': user[11],
        'group': getTranslation('Contacts'),
        'editor': 'text'
    });
    if (is_ru) {
        fields.push({
            'name': getTranslation('Icq'),
            'value': user[12],
            'group': getTranslation('Contacts'),
            'editor': 'text'
        });
    } else {
        fields.push({
            'name': getTranslation('Yahoo! Messenger'),
            'value': user[58],
            'group': getTranslation('Contacts'),
            'editor': 'text'
        });
    }
    fields.push({
        'name': getTranslation('Site'),
        'value': user[13],
        'group': getTranslation('Contacts'),
        'editor': 'text'
    });
    fields.push({
        'name': getTranslation('Facebook'),
        'value': user[15],
        'group': getTranslation('Contacts'),
        'editor': 'text'
    });
    if (!is_ru) {
        fields.push({
            'name': getTranslation('Google'),
            'value': user[59],
            'group': getTranslation('Contacts'),
            'editor': 'text'
        });
        fields.push({
            'name': getTranslation('Twitter'),
            'value': user[60],
            'group': getTranslation('Contacts'),
            'editor': 'text'
        });
    }
    fields.push({
        'name': getTranslation('Info'),
        'value': user[17],
        'group': getTranslation('About'),
        'editor': 'textarea'
    });
    editor_options['additional_contacts'] = user[18];
    fields.push({
        'name': getTranslation('Additional Contacts Title'),
        'value': '<span id="additional_contacts">' + user[18] + '</span>',
        'group': getTranslation('About'),
        'editor': (user[45] == 950) ? {
            'type': 'approveornew',
            'options': 'first_popup'
        }: 'textarea_nospan'
    });
    var max_i = 3;
    var i = 0;
    if (user[19] && user[19] != 0) {
        $.each(user[19],
        function(j, v) {
            if (i == 0) {
                $('.your_parent_name').html(v[3] + ' (' + v[1] + ')');
            }
            i++;
            max_i++;
            fields.push({
                'name': getTranslation('Name'),
                'value': v[1],
                'group': i
            });
            fields.push({
                'name': getTranslation('Status'),
                'value': toUsersPosition(v[2]),
                'group': i
            });
            fields.push({
                'name': getTranslation('Email'),
                'value': v[3],
                'group': i
            });
            if (v[4] && v[4].length) {
                fields.push({
                    'name': getTranslation('Mobile'),
                    'value': v[4],
                    'group': i
                });
            }
        });
    }
    if (user[56] && user['pr']) {
        var v = user['pr'];
        fields.push({
            'name': getTranslation('Name'),
            'value': v[0],
            'group': getTranslation('Present From')
        });
        fields.push({
            'name': getTranslation('Status'),
            'value': toUsersPosition(v[1]),
            'group': getTranslation('Present From')
        });
        fields.push({
            'name': getTranslation('Email'),
            'value': v[2],
            'group': getTranslation('Present From')
        });
        if (v[3] && v[3].length) {
            fields.push({
                'name': getTranslation('Mobile'),
                'value': v[3],
                'group': getTranslation('Present From')
            });
        }
    }
    if (user[50]) {
        $('.your_parent_name').html(user[50]);
    }
    $('#profile_edit').propertygrid('loadData', fields);
    return max_i;
}

function doCancel() {
    $('#profile_edit').propertygrid('rejectChanges');
}
function doSave() {
    var changes = $('#tt').propertygrid('getChanges');
    if (!changes.length) return false;
   
    var rows = $('#tt').propertygrid('getRows');
    var tmp = [];
    console.log(rows)
    $.each(rows,
    function(i, v) {
        if (i < 20) {
            tmp[v.name] = i;
        }
    });
    // var tmp2 = 'o=e';

    // var tmp2 = '';
    var tmp2 = {};
    $.each(changes,
    function(i, v) {
        console.log(v);
        var val = v.value;
        if (tmp[v.name] == region_index) {
            val = current_ir;
        }
        tmp2[v.key]=val;
        // tmp2 += '&' + v.key + '=' + val;
    });
    console.log(tmp2);
    $.ajax({
        url: '/Home/User/editUser',
        data: tmp2,
        success: function(data) {
            if (data.status==1) {
            	location.reload();
            }else {
            	showAlert(data.info);
            }
        }
    });
}
function initRefLink() {
    $('#ref_link').linkbutton();
}
function showRefLink() {
	//var link=$("#field_email").children("b").text();
	var link= '现在还写不了';
    var form = '';
    var k_host = window.location.host;
    form += '<div id="ref_link_dialog">';
    form += '<div style="padding:5px;"><input type="text" readonly="readonly" value=" '+k_host+'/Home/Reg/index/uname/'+link+'" style="width:99%;" /></div>';
    form += '</div>';
    form += '<div id="ref_link_dialog_buttons" iconCls="icon-info">';
    form += '<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="closeRefLink(); return false;">' + getTranslation('Close') + '</a>';
    form += '</div>';
    $(form).appendTo('body');
    $("#ref_link_dialog_buttons a").linkbutton();
    $('#ref_link_dialog').dialog({
        width: 700,
        height: 105,
        title: getTranslation('referals_my_referal_link'),
        modal: true,
        buttons: '#ref_link_dialog_buttons',
        onClose: closeRefLink,
        onOpen: function() {
            
        }
    });
}
function closeRefLink() {
    $('#ref_link_dialog').dialog('destroy');
}